//
//  SLBasicInformationViewController.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/20.
//


#import "SLBasicInformationViewController.h"
#import <YYKit.h>
#import <Masonry.h>
#import "SLProgressTableViewCell.h"
#import "SLInformationTableViewCell.h"

#import "SLBottomNextStepButton.h"
#import "OriginData.h"
#import "SLAuthenticationPush.h"

@interface SLBasicInformationViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIView *emptyView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) SLBottomNextStepButton *nextStepButton;
@property (nonatomic, copy  ) NSDictionary *data;

/// 地址选择的下标
@property (nonatomic, strong) NSMutableArray<NSNumber *> *indexs;


@end

@implementation SLBasicInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    @weakify(self);
    [self showNavBar:@"Basic Information" backBlock:^{
        @strongify(self);
        [SLAuthenticationPush popToProductDetailViewController:self];
    }];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view).inset(0);
        make.top.equalTo(self.navBar.mas_bottom).inset(0);
    }];
    
    [self.view addSubview:self.nextStepButton];
    [self.nextStepButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tableView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(100);
    }];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [Item2UserInfoRequestProvider requestItem2GetUserInfoWithProductID:self.productId success:^(NSDictionary * _Nonnull dict) {
        [MBProgressHUD hideHUDForView:self.view animated:NO];
        self.data = [dict deepMutableCopy];
        NSArray *guest = self.data[@"guest"];
        [self.tableView reloadData];
    } failure:^(YTKRequest * _Nonnull request) {
        [ResponseToast showToastWithRequest:request];
        [MBProgressHUD hideHUDForView:self.view animated:NO];
    }];
    Analysis.basic.start = NSDate.date;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        SLProgressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SLProgressTableViewCell" forIndexPath:indexPath];
        cell.progressView.stepCount = self.stepCount;
        cell.progressView.currentStep = self.currentStep;
        return cell;
    } else {
        SLInformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SLInformationTableViewCell" forIndexPath:indexPath];
        NSArray *guest = self.data[@"guest"];
        cell.items = guest;
        @weakify(self);
        cell.itemClickCallback = ^(NSInteger index) {
            @strongify(self);
            NSArray *guest = self.data[@"guest"];
            NSDictionary *dict = guest[index];
            NSString *cheerfully = dict[@"cheerfully"]; //表单类型
            NSString *formType = [OriginData cheerfullyOrigin:cheerfully];
            if ([formType isEqualToString:@"enum"]) {
                [self.view endEditing:YES];
                // 单选或多选
                NSArray *harp = dict[@"harp"];
                if (harp.count == 2) {
                    [SLToggleChooseView showToggleChooseWithData:harp titleCallback:^NSString * _Nonnull(NSInteger index) {
                        return harp[index][@"veins"];
                    } confirmCallback:^(NSInteger index) {
                        // 单选选择
                        [dict setValue:harp[index][@"veins"] forKey:@"lake"];
                        [dict setValue:harp[index][@"peppers"] forKey:@"peppers"];
                        [tableView reloadData];
                    }];
                } else {
                    [SLOptionChooseView showOptionCommonChooseWithCount:harp.count itemViewCallback:^(SLCommonOptionChooseCell * _Nonnull item, NSInteger index) {
                        item.titleLabel.text = harp[index][@"veins"];
                    } confirmCallback:^(NSInteger index) {
                        // 多选选择
                        [dict setValue:harp[index][@"veins"] forKey:@"lake"];
                        [dict setValue:harp[index][@"peppers"] forKey:@"peppers"];
                        [tableView reloadData];
                    }];
                }
            } else if ([formType isEqualToString:@"citySelect"]) {
                [self.view endEditing:YES];
                // 地址选择器
                [SLAddressPickerView showAddressChooseWithDefaultAddress:self.indexs confirmCallback:^(NSMutableArray<NSDictionary *> * _Nonnull select, NSMutableArray<NSNumber *> * _Nonnull indexs) {
                    self.indexs = indexs;
                    NSMutableArray *arr = [NSMutableArray array];
                    for (NSDictionary *dict in select) {
                        NSString *didn = dict[@"didn"];
                        NSString *veins = dict[@"veins"];
                        [arr addObject:veins];
                    }
                    NSString *address = [arr componentsJoinedByString:@"-"];
                    [dict setValue:address forKey:@"lake"];
                    [dict setValue:address forKey:@"peppers"];
                    [tableView reloadData];
                }];
            } else {
                // 输入框
            }
        };
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColor.whiteColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:SLProgressTableViewCell.class forCellReuseIdentifier:@"SLProgressTableViewCell"];
        [_tableView registerClass:SLInformationTableViewCell.class forCellReuseIdentifier:@"SLInformationTableViewCell"];
    }
    return _tableView;
}
- (SLBottomNextStepButton *)nextStepButton {
    if (!_nextStepButton) {
        _nextStepButton = [SLBottomNextStepButton new];
        @weakify(self);
        [_nextStepButton.nextStepButton jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            @strongify(self);
            [self.view endEditing:YES];
            NSMutableDictionary *info = [NSMutableDictionary dictionary];
            for (NSDictionary *dict in self.data[@"guest"]) {
                NSString *key = dict[@"weeps"];
                NSString *value = dict[@"peppers"];
                [info setValue:value forKey:key];
            }
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            [Item2UserInfoRequestProvider requestUpdateUserInfoWithProductId:self.productId info:info success:^(NSDictionary * _Nonnull dict) {
                [MBProgressHUD hideHUDForView:self.view animated:NO];
                Analysis.basic.end = NSDate.date;
                [SLLocationService startLocationWithCompletion:^(CGFloat lat, CGFloat lon) {
                    @strongify(self);
                    [Analysis sendDataWithProductID:self.productId time:Analysis.basic];
                }];
                
                [SLAuthenticationPush pushNextPageWithProductID:self.productId view:self.view];
            } failure:^(YTKRequest * _Nonnull request) {
                [MBProgressHUD hideHUDForView:self.view animated:NO];
                [ResponseToast showToastWithRequest:request];
            }];
        }];
    }
    return _nextStepButton;
}
@end
